package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fz.entity.RoleRes;
import com.fz.mapper.RoleResMapper;
import com.fz.service.RoleResService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 存储角色与权限对应关系 服务实现类
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@Service
public class RoleResServiceImpl extends ServiceImpl<RoleResMapper, RoleRes> implements RoleResService {
    @Override
    @Transactional
    public boolean save(Long roleId, Long[] menuIds, Long[] btnIds) {
        UpdateWrapper<RoleRes> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("role_id", roleId);
        this.remove(updateWrapper);
        List<RoleRes> list = new ArrayList<>();
        if(null != menuIds){
            for(Long resId : menuIds) {
                list.add(new RoleRes(roleId,resId,1));
            }
        }
        if(null != btnIds){
            for(Long resId : btnIds) {
                list.add(new RoleRes(roleId,resId,0));
            }
        }
        return this.saveBatch(list);
    }

    @Override
    public List<RoleRes> getByRoleId(Long roleId) {
        QueryWrapper<RoleRes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        return this.list(queryWrapper);
    }
}
